package com.letv.whatslive.model.convert;

import com.google.common.collect.Lists;
import com.letv.whatslive.model.BusinessCooperation;
import com.letv.whatslive.model.User;
import com.letv.whatslive.model.utils.ObjUtils;
import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by shenyue on 16-3-2.
 */
public class BusinessCooperationConvert {

    public static DBObject castBusinessCooperationToDBObject(BusinessCooperation businessCooperation) {
        DBObject dbo = new BasicDBObject();
        if (businessCooperation.getId() != null) {
            dbo.put("_id", businessCooperation.getId());
        }
        if(StringUtils.isNotBlank(businessCooperation.getCompanyName()))
            dbo.put("companyName", ObjUtils.toString(businessCooperation.getCompanyName()));
        dbo.put("companyType", ObjUtils.ifNull(businessCooperation.getCompanyType(), ""));
        dbo.put("companyAddress", ObjUtils.ifNull(businessCooperation.getCompanyAddress(), ""));
        if(StringUtils.isNotBlank(businessCooperation.getCompanyContacter()))
            dbo.put("companyContacter", ObjUtils.toString(businessCooperation.getCompanyContacter()));
        if(StringUtils.isNotBlank(businessCooperation.getCompanyContacterPost()))
            dbo.put("companyContacterPost", ObjUtils.toString(businessCooperation.getCompanyContacterPost()));
        if(StringUtils.isNotBlank(businessCooperation.getCompanyContacterPhoneNo()))
            dbo.put("companyContacterPhoneNo", ObjUtils.toString(businessCooperation.getCompanyContacterPhoneNo()));
        dbo.put("companyContacterEmail", ObjUtils.ifNull(businessCooperation.getCompanyContacterEmail(), ""));
        dbo.put("companyContacterQQ", ObjUtils.ifNull(businessCooperation.getCompanyContacterQQ(), ""));
        dbo.put("companyContacterWeixin", ObjUtils.ifNull(businessCooperation.getCompanyContacterWeixin(), ""));
        if(StringUtils.isNotBlank(businessCooperation.getOurContacterDepartment()))
            dbo.put("ourContacterDepartment", ObjUtils.toString(businessCooperation.getOurContacterDepartment()));
        if(StringUtils.isNotBlank(businessCooperation.getOurContacter()))
            dbo.put("ourContacter", ObjUtils.toString(businessCooperation.getOurContacter()));
        dbo.put("createTime", ObjUtils.toLong(businessCooperation.getCreateTime(), System.currentTimeMillis()));
        dbo.put("updateTime", ObjUtils.toLong(businessCooperation.getUpdateTime(), System.currentTimeMillis()));
        if(businessCooperation.getStartTime()!=null)
            dbo.put("startTime", ObjUtils.toLong(businessCooperation.getStartTime()));
        if(businessCooperation.getCooperationStatus()!=null)
            dbo.put("cooperationStatus",ObjUtils.toInteger(businessCooperation.getCooperationStatus()));
        if(businessCooperation.getCreateByPersonLoginName()!=null)
            dbo.put("createByPersonLoginName",ObjUtils.toString(businessCooperation.getCreateByPersonLoginName()));
        dbo.put("description", ObjUtils.ifNull(businessCooperation.getDescription(), ""));
        if(businessCooperation.getEndTime()!=null)
            dbo.put("endTime", ObjUtils.toLong(businessCooperation.getEndTime()));
        return dbo;
    }

    public static BusinessCooperation castDBObjectToBusinessCooperation(DBObject dbObject){
        if(dbObject != null) {
            BusinessCooperation businessCooperation = new BusinessCooperation();
            businessCooperation.setId(ObjUtils.toLong(dbObject.get("_id")));
            businessCooperation.setCompanyName(ObjUtils.toString(dbObject.get("companyName")));
            businessCooperation.setCompanyType(ObjUtils.toString(dbObject.get("companyType"), ""));
            businessCooperation.setCompanyAddress(ObjUtils.toString(dbObject.get("companyAddress"), ""));
            businessCooperation.setCompanyContacter(ObjUtils.toString(dbObject.get("companyContacter")));
            businessCooperation.setCompanyContacterPost(ObjUtils.toString(dbObject.get("companyContacterPost")));
            businessCooperation.setCompanyContacterPhoneNo(ObjUtils.toString(dbObject.get("companyContacterPhoneNo")));
            businessCooperation.setCompanyContacterEmail(ObjUtils.toString(dbObject.get("companyContacterEmail"), ""));
            businessCooperation.setCompanyContacterQQ(ObjUtils.toString(dbObject.get("companyContacterQQ"), ""));
            businessCooperation.setCompanyContacterWeixin(ObjUtils.toString(dbObject.get("companyContacterWeixin"), ""));
            businessCooperation.setOurContacterDepartment(ObjUtils.toString(dbObject.get("ourContacterDepartment")));
            businessCooperation.setOurContacter(ObjUtils.toString(dbObject.get("ourContacter")));
            businessCooperation.setCreateTime(ObjUtils.toLong(dbObject.get("createTime")));
            businessCooperation.setUpdateTime(ObjUtils.toLong(dbObject.get("updateTime")));
            businessCooperation.setStartTime(ObjUtils.toLong(dbObject.get("startTime")));
            businessCooperation.setCooperationStatus(ObjUtils.toInteger(dbObject.get("cooperationStatus")));
            businessCooperation.setDescription(ObjUtils.toString(dbObject.get("description"), ""));
            businessCooperation.setCreateByPersonLoginName(ObjUtils.toString(dbObject.get("createByPersonLoginName")));
            businessCooperation.setEndTime(ObjUtils.toLong(dbObject.get("endTime")));
            return businessCooperation;
        }else{
            return null;
        }

    }
}
